﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProcessor.EF;
using Hjt212Parser;
using log4net;


namespace DataProcessor.Process
{
    public class ProcessorStatus //: IProcessor
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(ProcessorStatus));

        public static void ProcessOffline(string content)
        {
            try
            {
                using (var contextTarget = new PlatformVocContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var info = Util.GetInfoFromMn(mn);
                    if (info == null)
                    {
                        Log.Warn("MN：" + mn + " 找不到对应企业！");
                        return;
                    }
                    data_realtime_monitor_main masterMonitor = //null;
                        contextTarget.data_realtime_monitor_main.Where(t => t.ps_code.Equals(info.ps_code)).OrderByDescending(t=>t.data_time)
                           .FirstOrDefault(t => t.pi_code.Equals(info.port_code));
                    if (masterMonitor == null)
                    {
                        masterMonitor = new data_realtime_monitor_main
                        {
                            ps_code = info.ps_code,
                            pi_code = info.port_code
                        };
                        contextTarget.data_realtime_monitor_main.Add(masterMonitor);
                    }
                    masterMonitor.pi_state = "离线";
                    masterMonitor.online_state = "离线";
                    masterMonitor.online_state_time = Parser.GetDateTimeByKey("DataTime", content);
                    contextTarget.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        //public static void ProcessOnline(string content)
        //{
        //    try
        //    {
        //        using (var contextTarget = new PlatformVocContext())
        //        {
        //            var mn = Parser.GetStringByKey("MN", content);
        //            if (string.IsNullOrEmpty(mn)) return;
        //            var info = Util.GetInfoFromMn(mn);
        //            if (info == null)
        //            {
        //                Log.Warn("MN：" + mn + " 找不到对应企业！");
        //                return;
        //            }
        //            data_realtime_monitor_main masterMonitor = null;
        //            //   contextTarget.data_realtime_monitor_main.Where(t => t.ps_code.Equals(info.ps_code))
        //            //       .FirstOrDefault(t => t.pi_code.Equals(info.port_code));
        //            if (masterMonitor == null)
        //            {
        //                masterMonitor = new data_realtime_monitor_main
        //                {
        //                    ps_code = info.ps_code,
        //                    pi_code = info.port_code
        //                };
        //                contextTarget.data_realtime_monitor_main.Add(masterMonitor);
        //            }
        //            masterMonitor.pi_state = "正常";
        //            masterMonitor.online_state = "在线";
        //            masterMonitor.online_state_time = Parser.GetDateTimeByKey("DataTime", content);
        //            contextTarget.SaveChanges();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Log.Error(ex);
        //    }
        //}

    }
}
